package com.cib.yym.homepage.common.impl;

import java.util.HashMap;
import java.util.Map;

import com.cib.yym.homepage.common.CacheManager;

/**
 * 默认使用Java HashMap做cache
 * @author ccc-yangyiming
 *
 */
public class DefaultCacheManager implements CacheManager{
		
	private static DefaultCacheManager instance = null;
	
	
	private Map<String,Map> cache = new HashMap();
	
	private DefaultCacheManager() {
	}

	public static synchronized DefaultCacheManager getInstance() {
		if (instance == null) {
			instance = new DefaultCacheManager();
		}
		return instance;
	}
	
	@Override	
	public Object getCachedData(String mapKey,String key)
	{		
		if(cache.get(mapKey) == null)
		{
			return null;
		}
		return cache.get(mapKey).get(key);
	}
	
	@Override
	public void updateCache(String mapName, String key, Object value) {
		try {
			Map tmp = cache.get(mapName);
			if(tmp == null)
			{
				tmp = new HashMap<String,Object>();
			}
			tmp.put(key, value);
			cache.put(mapName, tmp);
		} catch (Exception e) {
			e.printStackTrace();		
		}
		
	}
	
	@Override
	public void clearCache(String mapName)
	{
		cache.put(mapName, null);				
	}

	@Override
	public Map<String, Object> getCacheMap(String mapName) {
		return cache.get(mapName);
	}
	
}
